package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpCarTireArchivesMapper;
import com.monitor.dao.ErpCustomerCarMapper;
import com.monitor.dao.ErpInspectionContentMapper;
import com.monitor.dao.ErpInspectionOrderMapper;
import com.monitor.dao.ErpMaterielMapper;
import com.monitor.dao.ErpTireArchivesMapper;
import com.monitor.dao.ErpTireArchivesWorkMapper;
import com.monitor.entity.ErpCarTireArchives;
import com.monitor.entity.ErpCustomerCar;
import com.monitor.entity.ErpInspectionContent;
import com.monitor.entity.ErpMateriel;
import com.monitor.entity.ErpTireArchives;
import com.monitor.entity.ErpTireArchivesWork;
import com.monitor.service.ErpInspectionContentService;
import com.monitor.util.Result;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-30
 */
@Service
public class ErpInspectionContentServiceImpl extends ServiceImpl<ErpInspectionContentMapper, ErpInspectionContent> implements ErpInspectionContentService {

    @Resource
    private ErpInspectionContentMapper contentMapper;
    
    @Resource
    private ErpMaterielMapper erpMaterielMapper;
    
    @Resource
    private ErpInspectionContentMapper erpInspectionContentMapper;
    
    @Resource
    private ErpInspectionOrderMapper erpInspectionOrderMapper;
    
    @Resource
    private ErpTireArchivesWorkMapper erpTireArchivesWorkMapper;
    
    @Resource
    private ErpTireArchivesMapper erpTireArchivesMapper;
    
    @Resource
    private ErpCustomerCarMapper erpCustomerCarMapper;
    
    @Resource
    private ErpCarTireArchivesMapper erpCarTireArchivesMapper;

    @Override
    public Integer getMaxIdContent(String plate) {
        return contentMapper.getMaxIdContent(plate);
    }

    @Override
    public Double gettaiwenmohao(List<ErpInspectionContent> inspectionContents) {
        Double deepLinesNew = 0.00;
        Double deepCount = 0.00;
        for (ErpInspectionContent inspectionContent:inspectionContents) {
            if(inspectionContent.getDeepLines1() > 0){
                deepLinesNew += inspectionContent.getDeepLines1();
                deepCount += 1.00;
            }
            if(inspectionContent.getDeepLines2() > 0){
                deepLinesNew += inspectionContent.getDeepLines2();
                deepCount += 1.00;
            }
            if(inspectionContent.getDeepLines3() > 0){
                deepLinesNew += inspectionContent.getDeepLines3();
                deepCount += 1.00;
            }
            if(inspectionContent.getDeepLines4() > 0){
                deepLinesNew += inspectionContent.getDeepLines4();
                deepCount += 1.00;
            }

        }
        if(deepLinesNew > 0){
            deepLinesNew = deepLinesNew/deepCount;
        }

        return deepLinesNew;
    }

    @Override
    public Double getpinjuntaiwenshendu(ErpInspectionContent inspectionContent) {
        if(null==inspectionContent){
            return null;
        }
        Double shenduNum=inspectionContent.getDeepLines1()+inspectionContent.getDeepLines2()+inspectionContent.getDeepLines3()+inspectionContent.getDeepLines4();
        Double shendu=shenduNum/4;
        return shendu;
    }

    @Override
    public Integer getMaxIdContentByPro(String plate, String startDate, String endDate) {
        return contentMapper.getMaxIdContentByPro(plate,startDate,endDate);
    }

	@Override
	public Result<Map<String,Object>> selectByWuLiaoDangAn(String number,String carPlate,String name,String location) {
		// TODO Auto-generated method stub
		try {
			ErpCustomerCar car=erpCustomerCarMapper.selectByPlate(carPlate);
			List<ErpCarTireArchives> carTireList=erpCarTireArchivesMapper.selectList(new EntityWrapper<ErpCarTireArchives>().eq("car_id", car.getId()).and().eq("tire_location", location).orderBy("id", false));
			Map<String, Object> map=new HashMap<String, Object>();
			ErpMateriel materiel=null;
			List<Map<String, Object>> ipc=erpInspectionContentMapper.selectByProduceId(number);
			if(carTireList!=null&&carTireList.size()>0) {
				ErpCarTireArchives carTire =carTireList.get(0);
				materiel=erpMaterielMapper.selectById(carTire.getTireId());
				if(!materiel.getMaterielProductionId().trim().equals(number)||!materiel.getMaterielName().trim().equals(name)) {
					materiel=erpMaterielMapper.selectById(carTire.getSensorsId());
				}
			}
			List<ErpTireArchivesWork> list=new ArrayList<>();
			if(materiel!=null) {
				list=erpTireArchivesWorkMapper.selectList(new EntityWrapper<ErpTireArchivesWork>().eq("tire_id", materiel.getId()));
			}
			List<Map<String, Object>> ipcList=new ArrayList<Map<String,Object>>();
			Map<String, Object> totalMap=new HashMap<>();
			Double totalMileage=0.0;      //总运营里程
			Double totalYuJi=0.0;		//总预计运营里程
			Double totalGou=0.0;		//总购置翻新费用
			Double totalFee=0.0;		//总维修费用
			Double totalQian=0.0;		//总元/千公里
			for(int i=0;i<ipc.size();i++){
				Map<String, Object> ma=new HashMap<>();
				String states="";
				if(ipc.get(i).get("state")!=null) {
					states=ipc.get(i).get("state").toString();
					if(states.equals("")) {
						states="新物品";
					}
				}else {
					states="新物品";
				}
				ma.put("states", states);          //轮胎状态
				Double mileage=0.0;
				if(ipc.get(i).get("mileage")!=null) {
					mileage=Double.parseDouble(ipc.get(i).get("mileage").toString());
				}
				Double endMileage=0.0;
				if((i+1)<ipc.size()) {
					if(ipc.get(i+1).get("mileage")!=null) {
						endMileage=Double.parseDouble(ipc.get(i+1).get("mileage").toString());
					}
					mileage=endMileage-mileage;
				}
				totalMileage=totalMileage+mileage;
				ma.put("mileage", mileage);          //运营里程
				Double yuJi=0.0;
				if(ipc.get(i).get("tire_id")!=null) {
					if(!ipc.get(i).get("tire_id").toString().equals("")) {
						int tireId=Integer.parseInt(ipc.get(i).get("tire_id").toString());
						ErpTireArchives archives=erpTireArchivesMapper.selectByTireId(tireId);
						Double tai=0.0;
						if(ipc.get(i).get("deep_lines_1")!=null) {
							tai=Double.parseDouble(ipc.get(i).get("deep_lines_1").toString());
						}
						if(mileage!=0) {
							yuJi=(archives.getTireTread1()-tai)*(archives.getTireTread1()-1.6)/mileage;
						}else {
							yuJi=(archives.getTireTread1()-tai)*(archives.getTireTread1()-1.6);
						}
						
						ma.put("yuJi", yuJi);          //预计里程
					}else {
						ma.put("yuJi", 0);          //预计里程
					}
				}else {
					ma.put("yuJi", 0);          //预计里程
				}
				totalYuJi=totalYuJi+yuJi;
				Double gou=materiel.getMaterielStoragePrice();
				totalGou=totalGou+gou;
				Double fee=0.0;
				totalFee=totalFee+fee;
				if(ipc.get(i).get("all_fee")!=null) {
					fee=Double.parseDouble(ipc.get(i).get("all_fee").toString());
					gou=gou+fee;
				}
				ma.put("gou", gou);          //购置翻新
				ma.put("fee", fee);          //维修维护
				Double qian=0.0;
				if(yuJi!=0) {
					qian=(gou+fee)/yuJi*1000;
				}else {
					qian=(gou+fee)*1000;
				}
				totalQian=totalQian+qian;
				ma.put("qian", qian);          //元/千公里
				ipcList.add(ma);
			}
			totalMap.put("totalMileage", totalMileage);
			totalMap.put("totalYuJi", totalYuJi);
			totalMap.put("totalGou", totalGou);
			totalMap.put("totalFee", totalFee);
			totalMap.put("totalQian", totalQian);
			map.put("materiel", materiel);  //第一排和第二排数据
			map.put("totalMap", totalMap);  //第二排计算总和数
			map.put("inspectionList", ipcList);     //第三排数据
			map.put("sheetList", list);      //下面的工单数据列表
			return new Result<Map<String,Object>>(map, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}
}
